/**
 * Created by H2 on 2016/1/12.
 */

$(document).ready(function () {

    //定义url变量
    var userInfoURL = "/api/client/agent/agentlist";

    //定义分页变量
    var start = 0;
    var count = 5;
    var count_page = Math.ceil(20 / count);
    var nowPage = 1;


    //定义map
    var userMap = {
        "#userTemp" : "#user_content"
    }

    /* 调用ajax函数 */
    doRender(
        userInfoURL,
        {
            "start" : 0,
            "length": 5,
            "order" : 0,
            "t": now()
        }
        ,
        userMap
    );


    /* 调用分业按钮函数 */

    $(".btn_first").click(function(){
        if(nowPage == 1) return;
        nowPage = 1;
        $(".btn_this").eq(nowPage-1).addClass("btn_active").siblings().removeClass("btn_active");

        doRender(
            userInfoURL,
            {
                "start" : nowPage-1,
                "length": count,
                "order" : 0,
                "t": now()
            }
            ,
            userMap
        )
    })

    $(".btn_ending").click(function(){
        if(nowPage == count_page) return;
        nowPage = count_page;
        $(".btn_this").eq(nowPage-1).addClass("btn_active").siblings().removeClass("btn_active");

        doRender(
            userInfoURL,
            {
                "start" : (nowPage-1) * count,
                "length": count,
                "order" : 0,
                "t": now()
            }
                ,
                userMap
        )
    })

    $(".btn_next").click(function(){
        if(nowPage >= count_page) return;
        nowPage++;
        $(".btn_this").eq(nowPage-1).addClass("btn_active").siblings().removeClass("btn_active");

        doRender(
            userInfoURL,
            {
                "start" : (nowPage-1) * count,
                "length": count,
                "order" : 0,
                "t": now()
            }
            ,
            userMap
        )

    })

    $(".btn_before").click(function(){
        if(nowPage < 2) return;
        nowPage--;
        $(".btn_this").eq(nowPage-1).addClass("btn_active").siblings().removeClass("btn_active");

        doRender(
            userInfoURL,
            {
                "start" : (nowPage-1) * count,
                "length": count,
                "order" : 0,
                "t": now()
            }
            ,
            userMap
        )
    })

    $(".btn_this").click(function(){
        if(nowPage == Number($(this).text())) return;
        nowPage = Number($(this).text());
        $(this).addClass("btn_active").siblings().removeClass("btn_active");

        doRender(
            userInfoURL,
            {
                "start" : (nowPage-1) * count,
                "length": count,
                "order" : 0,
                "t": now()
            }
            ,
            userMap
        )
    })

})

